<?php
/*
$SNAME = session_name("JANSENSESSION");
if (isset($_COOKIE[$SNAME]) || isset($_GET[$SNAME])) {
$SID = isset($_COOKIE[$SNAME]) ? $_COOKIE[$SNAME] : $_GET[$SNAME];
session_id($SID);
} else {
$SID = session_id();
}

session_start();
setcookie(session_name(), session_id(), time()+3600*24*365, "/");
*/

require_once './include/common.inc.php';

//echo "<pre>";print_r($LEFT_MENU_ARY);exit;

checkLogin();
$postiondiv='<div class="position"><a href="index.php">首页</a> &gt; <span class="position"> 检视购物车 </span></div>';
$baner_img='<div class="bigtitle">检视购物车</div>';

$from_url = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $PHP_SELF;

$page_title = 'jans2en furniture - Container - Container';

getMyParam('act');
getMyParam('pid');
getMyParam('inventoryid');
getMyParam('inventorytype');
getMyParam('intype');
getMyParam('fitemid');
getMyParam('js');
$intype = (int)$intype;

if ($act == 'add' && is_numeric($pid)) 
{
	getMyParam('addnum');
	!isset($inventoryid) && $inventoryid =0;
	$sql = "SELECT * FROM `product` WHERE `id`='$pid'";
	$product_info_ary = $db->get_one($sql);
	$cart = new cart();
	$have_set_ary = $cart->getData();
	$id_str = $intype . '_' . $pid . '_'. $fitemid;	
	if (array_key_exists($id_str, $have_set_ary)) {
	    echo "<script>alert('产品".$product_info_ary['code']."已经存在于购物车中！');</script>";		
		exit;
	}	
	
	$sql = "SELECT `special_price` FROM `product_inventory` WHERE `product_id`='$pid' AND `inventory_id`='$inventoryid'";
	$tmp_ary = $db->get_one($sql);
	$sp = $tmp_ary['special_price'];
	!$sp && $sp=0;
	$name = $product_info_ary['name'];
	$price = getPrice($jansen_type, $inventorytype, $product_info_ary, $sp);

	$cart = new cart();
	$cart->addOne($pid, $name, $price, $product_info_ary['cbm'], $product_info_ary['cuft'],$fitemid, $inventoryid);

	if (is_numeric($addnum) && $addnum>0)
	{	
		$cart->modifyCount($id_str, $addnum);
	}
 	
	if ($js){
		echo "<script>alert('产品已加到购物车。');</script>";
		//exit;
	}
	saveCart($db, $jansen_uid, $cart);
}

if ($act == 'adddate' && is_array($_POST['p_num']))
 {
	foreach ($_POST['p_num'] as $k=>$v) {
		if (is_numeric($v) && $v>0) {
			$pid_ary[] = $k;
		}
	}
	if (!empty($pid_ary)) {
		$pid_str = implode(",", $pid_ary);
		$cart = new cart();
		$have_set_ary = $cart->getData();

		$sql = "SELECT `product_id`, `special_price` FROM `product_inventory` WHERE `product_id` IN (" . $pid_str . ") AND `inventory_id`='$inventoryid'";
		$query = $db->query($sql);
		while($rs = $db->fetch_array($query)) {
			$sp_ary[$rs['product_id']] = $rs['special_price'];
		}

		$sql = "SELECT * FROM `product` WHERE `id` IN (" . $pid_str . ")";
		$query = $db->query($sql);

		while($rs = $db->fetch_array($query)) {
			$id_str = $intype . '_' . $rs['id'];
			if (($inventorytype == 1 || $inventorytype == 2 )&& array_key_exists($id_str, $have_set_ary)) {
				
				echo "<script>alert('Item " . $rs['code'] . " 产品已经存在于购物车 !');</script>";
				exit;
			}

			$sp = isset($sp_ary[$rs['id']]) ? $sp_ary[$rs['id']] : 0;
			$price = getPrice($jansen_type, $inventorytype, $rs, $sp);			
			$data[] = array('id'=>$rs['id'], 'name'=>$rs['name'],'namezh'=>$rs['namezh'], 'price'=>$price, 'cbm'=>$rs['cbm'], 'cuft'=>$rs['cuft'], 'count'=>$_POST['p_num'][$rs['id']], 'intype'=>$intype);
		}		
		$cart = new cart();
		$cart->addData($data);

		saveCart($db, $jansen_uid, $cart);

		if ($js) {
			echo "<script>alert('产品已加到购物车。');parent.document.getElementById('addtocontentiframe').src='';parent.location.href='" . $from_url . "';</script>";
			exit;
		}
	}
}

if ($act == 'clearall') {
	$cart = new cart();
	$cart->emptyAll();
	@saveCart($db, $jansen_uid, "");
}

if ($act == 'delone') { 
	$cart = new cart();
	$cart->emptyOne($pid);
	if ($cart->getSortCount() == 0) {
		@saveCart($db, $jansen_uid, "");
	} else {
		@saveCart($db, $jansen_uid, $cart);
	}
}

if ($act == 'change') {
	getMyParam('selFinish');
	getMyParam('cart_qty');
	//echo "<pre>";print_r($cart_qty);
	$cart = new cart();
	
	foreach ($selFinish as $k=>$v) {
		if (!is_numeric($cart_qty[$k]) && trim($cart_qty[$k])!='') continue;
		else if($cart_qty[$k] <= 0 || trim($cart_qty[$k])=='') 
		{
			$cart->emptyOne($k);
		} 
		else
		{
			if($v == $fitemid[$k])
			{
				$cart->modifyCount($k, $cart_qty[$k]);
			}
			else
			{				
				$cart->modifyKey($k,$v,$cart_qty[$k]);
			}
		}		
	}
	
	/*foreach ($cart_qty as $k=>$v) {
		if (!is_numeric($v)) {
		} else if ($v <= 0) {
			$cart->emptyOne($k);
		} else {
			$cart->modifyCount($k, $v);
		}
	}*/
	
	//echo "<pre>";print_r($cart);exit;
	//echo $cart->getSortCount();exit;
	if ($cart->getSortCount() == 0) {
		@saveCart($db, $jansen_uid, "");
	} else {
		@saveCart($db, $jansen_uid, $cart);
	}
}


$cart = new cart();
$info_ary = $cart->getData();
$total_qty = $cart->getSortCount();
$total_cost = $cart->getTotalCost();
if ($_GET['debug']=='hugo222') {
	$total_cost = $cart->emptyAll();
}


$all_product_info_ary = array();
foreach ($info_ary as $pid=>$vv) 
{	
	$tpm_ary = explode("_", $pid);	
	$pid = $tpm_ary[1];
	$pid_ary[] = $pid;
}
$js_id_ary = array();
if (!empty($pid_ary)) {
	$pid_str = implode(",", $pid_ary);
	$sql = "SELECT `id`, `code`,`namezh`, `small_pic`, `pic`, `cbm`, `cuft`, `finish_id` FROM `product` WHERE id IN (" . $pid_str . ")";
	$query = $db->query($sql);
	while($rs = $db->fetch_array($query)) {
		//$rs['cbm'] = sprintf("%0.2f", $rs['cbm']);
		//$rs['cuft'] = sprintf("%0.2f", $rs['cuft']);
		$all_product_info_ary[$rs['id']] = $rs;		

	}

	$sql = "SELECT `product_id`,`inventory_id`, `qty` FROM `product_inventory` WHERE `product_id` IN (" . $pid_str . ") AND `qty`>=0";
	$query = $db->query($sql);
	while($rs = $db->fetch_array($query)) {
		$product_qty_ary[$rs['product_id']] = $rs['qty'];
		$js_id_ary[] = "'" . $rs['inventory_id'] . '_' . $rs['product_id'] . "'";
	}

}

$js_id_ary_str = !empty($js_id_ary) ? "new Array(" . implode(',', $js_id_ary) . ")" : "new Array()";

if ($jansen_type == 1) {
	$money_type = "<span class='EnTextFamily'> HKD</span>";
} else {
	$money_type = " 美金";
}

$sql = "SELECT * FROM `user` WHERE `id`='$jansen_uid'";
$user_info_ary = $db->get_one($sql);

$sql = "SELECT * FROM `inventory`";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
	$location_info_ary[$rs['id']] = $rs['name'];
}

$total_qty2 = $total_cbm = $total_cuft = 0;


$index_ary = array();
foreach ($info_ary as $k=>$v) {
	$location = $v['intype'] == 0 ? 'Products' : $location_info_ary[$v['intype']];
	$old_k = $k;

	$tpm_ary = explode("_", $k);
	$k = $tpm_ary[1];
	$js_str = $v['intype'] == 0 ? '' : '<input type="hidden" name="inventory_qty[' . $old_k . ']" value="' . $product_qty_ary[$k] . '">';
	if (trim($all_product_info_ary[$k]['small_pic']) == "" || !file_exists("attachments/" . $all_product_info_ary[$k]['small_pic'])) {
		$all_product_info_ary[$k]['small_pic'] = 'no_40.gif';
	}

	$index_ary[$v['intype'] . $all_product_info_ary[$k]['code'].'_'.$v['fitemid']] = array(
	'code' => $all_product_info_ary[$k]['code'],
	'pid' => $k,
	'location' => $location,
	'namezh' =>$all_product_info_ary[$k]['namezh'],
	'name' => $v['name'],
	'small_pic' => $all_product_info_ary[$k]['small_pic'],
	'cbm' => $all_product_info_ary[$k]['cbm'],
	'cuft' => $all_product_info_ary[$k]['cuft'],
	'price' => $v['price'],
	'count' => $v['count'],
	'cost' => $v['cost'],
	'intype' => $v['intype'],
	'finish_id' => $all_product_info_ary[$k]['finish_id'],
	'fitemid' => $v['fitemid'],
	'old_k' => $old_k,
	'js_str' => $js_str,
	'js_str' => $js_str,
	);

	$total_qty2 += $v['count'];
	$total_cbm += $v['count']*$all_product_info_ary[$k]['cbm'];
	$total_cuft += $v['count']*$all_product_info_ary[$k]['cuft'];
}

ksort($index_ary);

$total_cost  = number_format($total_cost, 2, '.', ',');
$total_cbm  = number_format(round($total_cbm), 0, '.', ',');
$total_cuft  = number_format(round($total_cuft), 0, '.', ',');
$jansen_total_qty1 = $total_qty;
$top_out = "&nbsp;&nbsp;" . $total_cbm . " CBM&nbsp;&nbsp;" . $total_cuft . " CUFT&nbsp;&nbsp;" . $total_cost . " $money_type";


require_once PrintEot('header');
require_once PrintEot('cart_view');
require_once PrintEot('footer');

?>